/*
 * Copyright 2018-9-30 20:11:44 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.core.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scp.erp.core.ContractManager;
import com.lap.scp.erp.dal.ContractDAO;
import com.lap.scp.erp.dal.PaymentDAO;
import com.lap.scp.erp.dal.ReceivablesDAO;
import com.lap.scp.erp.domain.ContractDO;
import com.lap.scp.erp.domain.PaymentDO;
import com.lap.scp.erp.domain.ReceivablesDO;
import com.lap.scp.erp.query.ContractQueryVO;

/**
 * <pre>
 * 合同信息业务代码默认实现类，这里提供最小元素原子操作，所以尽量少在该类中做流程代码。
 * 业务流程代码应该写在AO中。
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-9-30 20:11:44
 * @since 2.0.0
 */
@Service("contractManager")
public class DefaultContractManager implements ContractManager {
	/**
	 * 合同信息数据操作对象
	 */
	@Resource
	private ContractDAO contractDAO;

	@Resource
	private PaymentDAO paymentDAO;

	@Resource
	private ReceivablesDAO receivablesDAO;

	@Override
	public void insertContract(ContractDO contractDO) throws ManagerException {
		contractDAO.insertContract(contractDO);
	}

	@Override
	public int deleteById(Integer contractId) throws ManagerException {
		return contractDAO.deleteById(contractId);
	}

	@Override
	public ContractDO queryById(Integer contractId) throws ManagerException {
		ContractDO contractDO = contractDAO.queryById(contractId);
		if (contractDO != null) {
			List<ReceivablesDO> receiList = receivablesDAO.queryByContractId(contractId);
			contractDO.setReceivableList(receiList);

			List<PaymentDO> payList = paymentDAO.queryTotalByContractId(contractId);
			contractDO.setPaymentList(payList);
		}
		return contractDO;
	}

	@Override
	public ContractDO queryReceiById(Integer contractId) throws ManagerException {
		ContractDO contractDO = contractDAO.queryById(contractId);
		if (contractDO != null) {
			List<ReceivablesDO> receiList = receivablesDAO.queryByContractId(contractId);
			contractDO.setReceivableList(receiList);
		}
		return contractDO;
	}

	@Override
	public ContractDO queryDetailById(Integer contractId, Integer type) throws ManagerException {
		ContractDO contractDO = contractDAO.queryById(contractId);
		if (contractDO != null) {
			List<PaymentDO> paymentList = paymentDAO.queryByContractIdAndType(contractId, type);
			contractDO.setPaymentList(paymentList);
		}
		return contractDO;
	}

	@Override
	public ContractDO queryStatisByCustomerId(Integer customerId) throws ManagerException {
		return contractDAO.queryStatisByCustomerId(customerId);
	}

	@Override
	public List<ContractDO> queryPage(Page page, ContractQueryVO queryVO) throws ManagerException {
		int count = contractDAO.countPage(queryVO);
		if (count == 0) {
			return Collections.emptyList();
		} else {
			page.setTotalItem(count);
			return contractDAO.queryPage(page, queryVO);
		}
	}

	@Override
	public int updateContract(ContractDO contractDO) throws ManagerException {
		return contractDAO.updateContract(contractDO);
	}

	@Override
	public List<ContractDO> queryList() throws ManagerException {
		return contractDAO.queryList();
	}

}
